// history模式
import { createRouter, createWebHashHistory } from 'vue-router'
import routes_main from './routes_main.js'
import conditionMonitoring from './routes_conditMon.js'
import parameterConfiguration from './routes_parameterConfig.js'
import { mainStore } from '../store/mainStore'

// 将路由合并
let routes = [...routes_main, ...conditionMonitoring, ...parameterConfiguration]

// 创建路由对象
const router = createRouter({
  history: createWebHashHistory(),
  routes: routes,
})

// 全局守卫：登录拦截 本地没有存token,请重新登录
// 在全局设置一个标志，表示当前是否正在进行导航
router.beforeEach((to, from, next) => {
  next()
})

// 成功跳转后监听当前路由
router.afterEach((to, from) => {
  // to 是进入的路由对象
  // from 是离开的路由对象
  // 拿到当前路由名称 方便在其他页面时 暂停三维页面的渲染

  const store = mainStore()
  store.currentRoute = to.matched[0].name // 只要主路由的name
  store.saveAllData = []
  if (store.currentRoute !== 'perceivedEffect') {
    store.carWsConnect?.close()
  }

  if (to.matched[0].name == 'parameterConfiguration') {
    const key = to.matched[1].name

    switch (key) {
      case 'transportation':
        switch (store.tranTitle) {
          case 1:
            store.cutArgPage = 'event-param'
            break
          case 2:
            store.cutArgPage = 'event-enable'
            break
        }
        break

      case 'devicedriver':
        store.cutArgPage = 'device-driver-rsu'
        break

      case 'v2xcomponent':
        switch (store.eventTitle) {
          case 1:
            store.cutArgPage = 'v2x-app'
            break
          case 2:
            store.cutArgPage = 'v2x-prostack'
            break
        }
        break

      case 'deepstream':
        store.cutArgPage = 'deep-stream-application'
        break

      case 'operation':
        store.cutArgPage = 'monitor-threshold'
        break

      case 'equipmentsystem':
        store.cutArgPage = 'device-system'
        break

      case 'perceptioncomponent': // 感知相关的配置
        switch (store.perTitle) {
          case 1:
            store.cutArgPage = 'perception-obstacles-polefusion-soft'
            break
          case 2:
            store.cutArgPage = 'perception-obstacles-soft'
            break
          case 3:
            store.cutArgPage = 'perception-obstacles-polefusion-alg'
            break
          case 4:
            store.cutArgPage = 'perception-obstacles-alg'
            break
        }
        break

      default:
        store.cutArgPage = null
        break
    }
  }
})

export default router
